package com.tianye.lc.activity;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.tianye.lc.R;
import com.tianye.lc.Tools;
import com.tianye.lc.adapter.BreedRecyclerAdapter;
import com.tianye.lc.adapter.YuhuaguListRecyclerAdapter;
import com.tianye.lc.common.Final;

import java.util.List;

public class YuhuaguRoseListActivity extends BaseActivity {
    public static final int REQUEST_ROSE_DETAIL = 0;

    private RecyclerView recycler;
    private YuhuaguListRecyclerAdapter adapter;

    private EditText edit;

//    private List<AVObject> botanyList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yuhuagu_rose_list);
        initTitle();
        initView();
        initData();
    }

    private void initTitle() {
        final CheckBox name_cb = (CheckBox) findViewById(R.id.name_cb);
        final TextView title = (TextView) findViewById(R.id.title_tv);
        title.setText("浴花谷月季资料");
        edit = (EditText) findViewById(R.id.title_et);
        final TextView search = (TextView) findViewById(R.id.right_tv);
        search.setText("搜索");
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (title.getVisibility() == View.VISIBLE) {
                    title.setVisibility(View.GONE);
                    edit.setVisibility(View.VISIBLE);
                    edit.requestFocus();
                    search.setText("全部");
                    name_cb.setVisibility(View.VISIBLE);
                    Tools.showSoftInput(edit);
                } else {
                    title.setVisibility(View.VISIBLE);
                    edit.setVisibility(View.GONE);
                    edit.setText("");
                    search.setText("搜索");
                    name_cb.setVisibility(View.GONE);
                    Tools.hideSoftInput(edit);
                }
            }
        });
        edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                adapter.filter(edit.getText().toString(), name_cb.isChecked());
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        name_cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                adapter.filter(edit.getText().toString(), name_cb.isChecked());
            }
        });
    }

    @Override
    protected int getTitleLayout() {
        return R.layout.title_view_rose_list;
    }

    private void initView() {
        recycler = (RecyclerView) findViewById(R.id.recycler);
        recycler.setHasFixedSize(true);
        recycler.setLayoutManager(new LinearLayoutManager(this));
        recycler.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                //设定底部边距为1px
                outRect.set(0, 0, 0, 1);
            }
        });
        recycler.setHasFixedSize(true);

        adapter = new YuhuaguListRecyclerAdapter(this);
        recycler.setAdapter(adapter);

        edit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                Tools.hideSoftInput(edit);
                return false;
            }
        });
    }

    private void initData() {
//        String botanyListStr = Tools.readStringFile("botanyList.txt");
        String botanyListStr = null;
        if (TextUtils.isEmpty(botanyListStr)) {
            loadRoseListFromNet();
        } else {
            List<AVObject> botanyList = JSON.parseArray(botanyListStr, AVObject.class);
            adapter.setData(botanyList);
        }
    }


    private void loadRoseListFromNet() {
        AVQuery<AVObject> query = new AVQuery<AVObject>(Final.BOTANY);
        query.limit(1000);// 最多返回 1000 条结果
        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
//                String botanyListStr = JSON.toJSONString(list);
//                Tools.writeStringFile("botanyList.txt", botanyListStr);
                adapter.setData(list);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case REQUEST_ROSE_DETAIL:
                    if (null != data) {
                        boolean like = data.getBooleanExtra("like", false);
                        String id = data.getStringExtra("rose_id");
                        adapter.updateListLike(id, like);
                        adapter.notifyDataSetChanged();
                    }
                    break;
            }
        }
    }

}
